import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入组件
import Reg from '@/views/Reg/Reg.vue'
import Login from '@/views/Login/Login.vue'
import Main from '@/views/Main/Main.vue'
import Home from '@/views/Manus/Home/Home.vue'
import store from '@/store'
import UserInfo from '@/views/Manus/user/UserInfo'
import UserAvatar from '@/views/Manus/user/UserAvatar'
import UserPwd from '@/views/Manus/user/UserPwd'
import ArtCate from '@/views/Manus/Article/ArtCate'
import ArtList from '@/views/Manus/Article/ArtList'
Vue.use(VueRouter)
// 设置规则
const routes = [
  { path: '/reg', component: Reg },
  { path: '/login', component: Login },
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [
      { path: 'home', component: Home },
      { path: 'user-info', component: UserInfo },
      { path: 'user-avatar', component: UserAvatar },
      { path: 'user-pwd', component: UserPwd },
      { path: 'art-cate', component: ArtCate },
      { path: 'art-list', component: ArtList }
    ]
  }
]
const router = new VueRouter({
  routes
})
// 设置路由全局守卫
router.beforeEach((to, from, next) => {
  // 需求：如果没有登录
  const token = store.state.user.token
  const whiteList = ['/login', '/reg']
  // if (!token && to.path !== '/login' && to.path !== '/reg') {
  //   next('/login')
  // }
  if (!token && !whiteList.includes(to.path)) {
    next('/login')
    Vue.prototype.$message.error('您还没有登录')
  } else {
    next()
  }
})
export default router
